import treeSearchMixin from './treeSearchMixin'

import api from '@/odooapi'
// import { try_call } from '@/odooapi/tools'

// eslint-disable-next-line no-unused-vars
const cp = val => JSON.parse(JSON.stringify(val))

const PageSize = 6

export default {
  mixins: [treeSearchMixin],

  props: {},

  data() {
    return {
      loading: false,

      pagination: {
        // position: 'top'
        total: 0,
        pageSize: PageSize
      },
      //

      activeIdsWithGroupby: [],
      expandedRowKeys: []
    }
  },
  computed: {
    viewType() {
      return 'list'
    },

    pagination2() {
      const { pagination } = this.data
      if (pagination) return pagination

      return { current: 1, total: 0, pageSize: PageSize }
    }
  },
  watch: {
    pagination2: {
      // eslint-disable-next-line no-unused-vars
      handler: function (newVal, oldval) {
        // console.log('watch, pagination2, val', newVal, oldval)
        // this.$emit('update:searchValue', newVal)
        this.pagination = JSON.parse(JSON.stringify(newVal))
      },
      deep: true,
      immediate: true
    }
  },

  async created() {},

  mounted() {},

  methods: {
    async load_data(search) {
      // console.log('load_data', [search, this.searchValue])
      this.data = await api.Views[this.viewType].load_data(this.viewInfo2, {
        search: search || this.searchValue,
        pagination: this.pagination
      })
      // console.log(cp(this.data), this.pagination)
    },

    async handlePageChange(page) {
      // console.log(page)
      this.pagination = { ...this.pagination, current: page }
      this.load_data()
    }
  }
}
